from django.shortcuts import render
import pandas as pd
from .ana import ChipotleAna
from pyecharts import Pie, Bar, Gauge, EffectScatter, WordCloud
from django.http import HttpResponse
from pyecharts import Liquid, Polar, Radar

df = pd.read_csv("exDAta/chipotle.csv", sep="\t")

# 业绩达标率 里程表
def performance(request):

    # 总共挣钱
    total = df["item_price"].str.replace("$", "")
    total_f = pd.to_numeric(total)
    total_f.sum()

    # 里程表
    # 理想业绩50000

    total_z = total_f.sum()
    yeji = total_z / 50000 * 100

    gauge = Gauge("达标的业绩",width=1800, height=1000)

    gauge.add("达标的业绩", "达标率", round(yeji))

    # gauge.render("exData/templates/performance.html")
    return render(request,"performance.html")
    # return HttpResponse("成功")

# 物品订购次数 饼图
def order(request):
    names = df["item_name"].value_counts()

    attr = []
    #列表
    for i in names.index:
        # print(i)
        attr.append(i)

    v1 = []
    for j in names:
        # print(j)
        v1.append(j)

    # 标题
    pie = Pie("订购物品次数", width=1800, height=1000)

    pie.add("",attr,v1,is_label_show=True)


    # 显示配置
    # pie.show_config()

    # pie.render("exData/templates/order.html")

    return render(request,"order.html")


# 关键字 柱状图
def keyword(request):
    # 用户关键字的出现次数

    dfs = df["choice_description"].value_counts()
    # display(dfs,df)


    attr=[]
    for i in dfs.index[:25]:
        attr.append(i)
    v1 = []
    for j in dfs[:25]:
        v1.append(j)


    bar = Bar("关键字",background_color='white',title_pos='right',width=1200, height=1000)
    bar.add("", attr, v1,is_stack=True)
    # bar.render("exData/templates/keyword.html")
    return render(request,"keyword.html")

def polars(request):

    dfss = df["choice_description"].value_counts()
    radius = []
    for q in dfss.index[:25]:
        radius.append(q)

    shuliang = []
    for w in dfss[:25]:
        shuliang.append(w*100)
    polar =Polar("极坐标", width=1000, height=1200)
    polar.add("占据数量", shuliang,radius_data=radius, type='barRadius', is_stack=True)

    # polar.render("exData/templates/polars.html")
    return render(request,"polars.html")

def index(request):

    return render(request,'index.html')
